package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.WmSensitiveService;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author 27639
* @description 针对表【wm_sensitive(敏感词信息表)】的数据库操作Service实现
* @createDate 2023-07-14 15:59:25
*/
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService{

    @Override
    public ResponseResult deleteSensitive(Integer id) {
        boolean b = removeById(id);
        return b ? ResponseResult.okResult(200,"删除敏感词成功") : ResponseResult.errorResult(6666,"删除失败");
    }

    @Override
    public ResponseResult listSensitive(SensitiveDto dto) {
        //1.检查参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        dto.checkParam();

        //2.分页条件查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();


        //关键字模糊查询
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmSensitive::getSensitives,dto.getName());
        }

        page = page(page,lambdaQueryWrapper);

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult saveSensitive(WmSensitive sensitive) {
        boolean save = save(sensitive);
        return save ? ResponseResult.okResult(200,"添加敏感词成功") : ResponseResult.errorResult(6666,"添加失败");

    }

    @Override
    public ResponseResult updateSensitive(WmSensitive sensitive) {
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        boolean b = update(sensitive, wrapper.eq(WmSensitive::getId, sensitive.getId()));
        return b ? ResponseResult.okResult(200,"更新敏感词成功") : ResponseResult.errorResult(6666,"更新失败");
    }
}




